﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Linq.Expressions;

namespace Relocation.Com
{
	public static partial class Expend
	{
		/// <summary>
		/// 返回异常信息的描述文本（如果有内部异常，则返回内部异常的描述文本）
		/// </summary>
		/// <param name="source"></param>
		/// <returns></returns>
		public static string GetInnerExceptionMessage(this Exception source)
		{
			return source.InnerException == null ? source.Message : source.InnerException.Message;			
		}

		public static int ToInt(this int? source)
		{ 
			return source==null?0:(int)source;
		}

		public static decimal ToDecimal(this decimal? source)
		{
			return source == null ? 0 : (decimal)source;
		}

		public static string ToDecimalString(this decimal? source)
		{
			return source.ToDecimal().ToString("0.00");
		}

		public static string ToString(this DateTime? source,string formatString)
		{
			if (string.IsNullOrEmpty(formatString))
				formatString = "yyyy年MM月dd日";
			return source.HasValue ? source.Value.ToString(formatString) : string.Empty;
		}


		/// <summary>
		/// 判断当前decimal?是否为空或者0
		/// </summary>		
		public static bool IsNullOrZero(this decimal? source)
		{
			return source.HasValue || source.Value==0;
		}

		/// <summary>
		/// 将单引号转换成两个单引号
		/// </summary>
		/// <param name="source"></param>
		/// <returns></returns>
		static public string GetSQLSafe(this string source)
		{
			return source.Replace("'", "''");
		}

		/// <summary>
		/// 比较两个字符串是否相等，不同的是此比较将null和""（string.Empty）视为相等
		/// </summary>
		/// <param name="source"></param>
		/// <param name="tagString"></param>
		/// <returns></returns>
		static public bool EqualsNullable(this string source,string tagString)
		{
			if (string.IsNullOrEmpty(source) && string.IsNullOrEmpty(tagString))
				return true;
			return source.Equals(tagString);

		}

		/// <summary>
		///   在拥有此控件的基础窗口句柄的线程上执行指定的委托。
		/// </summary>
		/// <param name="source"></param>
		/// <param name="methodInvoker"></param>
		static public void ExecInvoke(this Control source, MethodInvoker methodInvoker)
		{
			if (methodInvoker == null)
				return;
			if (source.Created && source.InvokeRequired)
			{
				source.Invoke(methodInvoker);
			} else
			{
				methodInvoker();
			}
		}

		static public bool IsOK(this DialogResult source)
		{
			return DialogResult.OK.Equals(source);
		}

		static public bool IsYes(this DialogResult source)
		{
			return DialogResult.Yes.Equals(source);
		}
			 
	}
}
